const BASE_URL = 'http://1.14.98.17:8765';

// 通用请求函数
async function request(url, method = 'GET', data = null) {
  const options = {
    method,
    headers: {
      'Content-Type': 'application/json',
    },
  };

  if (data) {
    options.body = JSON.stringify(data);
  }

  try {
    const response = await fetch(`${BASE_URL}${url}`, options);
    const result = await response.json();
    
    if (!result.success) {
      throw new Error(result.message || '请求失败');
    }
    
    return result.data;
  } catch (error) {
    console.error(`API请求错误: ${url}`, error);
    throw error;
  }
}

// 用户相关API
export async function fetchUsers() {
  return request('/api/users');
}

export async function fetchUser(userId) {
  return request(`/api/users/${userId}`);
}

export async function createUser(userData) {
  return request('/api/users', 'POST', userData);
}

export async function updateUser(userId, userData) {
  return request(`/api/users/${userId}`, 'PUT', userData);
}

export async function deleteUser(userId) {
  return request(`/api/users/${userId}`, 'DELETE');
}

export async function fetchUserNotes(userId) {
  return request(`/api/users/${userId}/notes`);
}

export async function fetchUserComments(userId) {
  return request(`/api/users/${userId}/comments`);
}

// 笔记相关API
export async function fetchNotes(params = {}) {
  const query = new URLSearchParams(params).toString();
  return request(`/api/notes?${query}`);
}

export async function fetchNote(noteId) {
  return request(`/api/notes/${noteId}`);
}

export async function createNote(noteData) {
  return request('/api/notes', 'POST', noteData);
}

export async function updateNote(noteId, noteData) {
  return request(`/api/notes/${noteId}`, 'PUT', noteData);
}

export async function deleteNote(noteId) {
  return request(`/api/notes/${noteId}`, 'DELETE');
}

export async function fetchNoteMedia(noteId) {
  return request(`/api/notes/${noteId}/media`);
}

export async function fetchNoteComments(noteId) {
  return request(`/api/notes/${noteId}/comments`);
}

export async function likeNote(noteId) {
  return request(`/api/notes/${noteId}/like`, 'POST');
}

export async function favoriteNote(noteId) {
  return request(`/api/notes/${noteId}/favorite`, 'POST');
}

// 媒体文件相关API
export async function fetchMedia() {
  return request('/api/media');
}

export async function fetchMediaByType(mediaType) {
  return request(`/api/media/type/${mediaType}`);
}

export async function uploadMedia(formData) {
  // 特殊处理文件上传
  try {
    const response = await fetch(`${BASE_URL}/api/media`, {
      method: 'POST',
      body: formData,
    });
    const result = await response.json();
    
    if (!result.success) {
      throw new Error(result.message || '上传失败');
    }
    
    return result.data;
  } catch (error) {
    console.error('媒体上传错误', error);
    throw error;
  }
}

export async function updateMedia(mediaId, mediaData) {
  return request(`/api/media/${mediaId}`, 'PUT', mediaData);
}

export async function deleteMedia(mediaId) {
  return request(`/api/media/${mediaId}`, 'DELETE');
}

// 评论相关API
export async function fetchComments() {
  return request('/api/comments');
}

export async function fetchComment(commentId) {
  return request(`/api/comments/${commentId}`);
}

export async function createComment(commentData) {
  return request('/api/comments', 'POST', commentData);
}

export async function updateComment(commentId, commentData) {
  return request(`/api/comments/${commentId}`, 'PUT', commentData);
}

export async function deleteComment(commentId) {
  return request(`/api/comments/${commentId}`, 'DELETE');
}

export async function fetchCommentReplies(commentId) {
  return request(`/api/comments/${commentId}/replies`);
}

export async function createCommentReply(commentId, replyData) {
  return request(`/api/comments/${commentId}/replies`, 'POST', replyData);
}

export async function likeComment(commentId) {
  return request(`/api/comments/${commentId}/like`, 'POST');
}

// 社交互动API
export async function followUser(userId) {
  return request(`/api/users/${userId}/follow`, 'POST');
}

export async function shareNote(noteId, platform) {
  return request(`/api/notes/${noteId}/share`, 'POST', { platform });
}